using System.Collections.Generic;
using ICSharpCode.TextEditor.Document;

namespace ICSharpCode.TextEditor.Src.Document.FoldingStrategy
{
	public class CSharpFoldingStrategy : IFoldingStrategy
	{
		public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
		{
			List<FoldMarker> list = new List<FoldMarker>();
			Stack<int> stack = new Stack<int>();
			for (int i = 0; i < document.TotalNumberOfLines; i++)
			{
				LineSegment lineSegment = document.GetLineSegment(i);
				int textLength = document.TextLength;
				int j;
				for (j = lineSegment.Offset; j < textLength; j++)
				{
					char charAt;
					if ((charAt = document.GetCharAt(j)) != ' ' && charAt != '\t')
					{
						break;
					}
				}
				if (j == textLength)
				{
					break;
				}
				int num = j - lineSegment.Offset;
				if (document.GetCharAt(j) == '#')
				{
					string text = document.GetText(j, lineSegment.Length - num);
					if (text.StartsWith("#region"))
					{
						stack.Push(i);
					}
					if (text.StartsWith("#endregion") && stack.Count > 0)
					{
						int num2 = stack.Pop();
						list.Add(new FoldMarker(document, num2, document.GetLineSegment(num2).Length, i, num + "#endregion".Length, FoldType.Region, "{...}"));
					}
				}
				if (document.GetCharAt(j) == '{')
				{
					int num3 = document.FormattingStrategy.SearchBracketForward(document, j + 1, '{', '}');
					if (num3 > 0)
					{
						int length = num3 - j + 1;
						list.Add(new FoldMarker(document, j, length, "{...}", isFolded: false));
					}
				}
				if (document.GetCharAt(j) == '/')
				{
					string text2 = document.GetText(j, lineSegment.Length - num);
					if (text2.StartsWith("/// <summary>"))
					{
						stack.Push(i);
					}
					if ((text2.StartsWith("/// <param") || text2.StartsWith("/// <returns>") || text2.StartsWith("/// </summary>")) && stack.Count > 0)
					{
						int num4 = stack.Pop();
						list.Add(new FoldMarker(document, num4, document.GetLineSegment(num4).Length, i, num + "/// </summary>".Length, FoldType.TypeBody, "/// <summary>..."));
					}
				}
			}
			return list;
		}
	}
}
